﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using hachiModel;

namespace Hachi.Controllers
{
    public class CategoriesController : BaseController
    {
        public CategoriesController()
        { 
        }
        
        public List<PR_Category> GetCategoriesCanUse()
        {
            return en.PR_Category.Where(d => d.IsApproved == true && d.IsDeleted == false).ToList();
        }
        public List<PR_Category> GetRootCategoriesCanUse()
        {
            return en.PR_Category.Where(d => d.IsApproved == true && d.IsDeleted == false && d.ParentID==null).OrderBy(o=>o.SortOrder).ToList();
        }
        public List<PR_Category> GetChildrenCategoriesCanUse(int ParentID)
        {
            return en.PR_Category.Where(d => d.IsApproved == true && d.IsDeleted == false && d.ParentID == ParentID).OrderBy(o => o.SortOrder).ToList();
        }
        public PR_Category getCategory(string alias, string Lang)
        {
            PR_CategoryDetail cd = en.PR_CategoryDetail.Where(d => d.Alias == alias && d.Language == Lang).FirstOrDefault();
            return cd.PR_Category;
        }
        public PR_Category getCategory(int pCategoryID, string Lang)
        {
            PR_CategoryDetail cd = en.PR_CategoryDetail.Where(d => d.CategoryID == pCategoryID && d.Language == Lang).FirstOrDefault();
            return cd.PR_Category;
        }
        public PR_Category getCategory(int CategoryID)
        {
            return en.PR_Category.Where(d => d.CategoryID == CategoryID).FirstOrDefault();
        }
        public int CountProduct(int CategoryID,string Lang, decimal FromPrice, decimal ToPrice)
        {
            PR_Category c = en.PR_Category.Where(d => d.CategoryID == CategoryID).FirstOrDefault();
            if (c!=null && c.ParentID!=null)
            {
                return en.PR_Product.Where(d => 
                    d.CategoryID == CategoryID
                    && d.PR_ProductDetail.Where(dd => dd.Language == Lang).Count() > 0 
                    && d.IsDeleted == false 
                    && d.IsApproved == true
                    && d.Quantity>0
                    && d.Price >= FromPrice && d.Price <= ToPrice
                    ).Count();
            }
            else if (c!=null && c.ParentID==null)
            {
                List<int> idl = new List<int>();
                foreach (PR_Category item in c.PR_Category1.ToList())
                    idl.Add(item.CategoryID);

                return en.PR_Product.Where(d =>
                    idl.Contains(d.CategoryID)
                    && d.PR_ProductDetail.Where(dd => dd.Language == Lang).Count() > 0
                    && d.IsDeleted == false
                    && d.IsApproved == true
                    && d.Quantity > 0
                    && d.Price >= FromPrice && d.Price <= ToPrice
                    ).Count();
            }

            return 0;
        }
        public PR_CategoryDetail getCategoryDetail(int CategoryID, string Language)
        {
            return en.PR_CategoryDetail.Where(d => d.CategoryID == CategoryID && d.Language == Language).FirstOrDefault();
        }
        public bool BuildAlias()
        {
            try
            {
                foreach (var c in en.PR_Category.Where(d => d.ParentID == null))
                {
                    foreach (var cd in c.PR_CategoryDetail)
                    {
                        if (cd.Language=="ja-JP")
                            cd.Alias = "Products⁄" + cd.CategoryName.Trim().Replace(" ", "-");
                        else
                            cd.Alias = "Products⁄" + StringUtil.RemoveVietSignSpecialCharAndSpaceChar(cd.CategoryName);
                    }
                }
                foreach (var c in en.PR_Category.Where(d => d.ParentID != null))
                {
                    foreach (var cd in c.PR_CategoryDetail)
                    {
                        if (cd.Language == "ja-JP")
                            cd.Alias = getCategoryDetail(c.ParentID.Value, cd.Language).Alias + "⁄"
                                + cd.CategoryName.Trim().Replace(" ", "-");
                        else
                            cd.Alias = getCategoryDetail(c.ParentID.Value, cd.Language).Alias + "⁄" 
                                + StringUtil.RemoveVietSignSpecialCharAndSpaceChar(cd.CategoryName);
                    }
                }
                en.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}